﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using RLPUR.Common;
using RLPUR.Models;

namespace RLPUR.Web
{
    public partial class PurStatement : LocalPage
    {
        /// <summary>
        /// 页面加载
        /// </summary>
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.IsPostBack)
            {
                #region 验证权限

                #endregion

                #region 获取参数

                #endregion

                //初始化
                this.Initialize();
            }

            #region 页面标题

            this.Title = PageTitle.Text;

            #endregion

            #region 页面要素


            #endregion
        }

        #region 初始化

        /// <summary>
        /// 初始化
        /// </summary>
        private void Initialize()
        {
            #region 页面内容

            OrNo.Text = string.Empty;
            VenNo.Text = string.Empty;
            VenName.Text = string.Empty;
            DateFrom.Text = DateTime.Now.AddMonths(-1).ToString("yyyy-MM-dd");
            DateTo.Text = DateTime.Now.ToString("yyyy-MM-dd");

            IsTon.Checked = false;
            IsTon_OnCheckedChanged(this, null);

            #endregion

            //绑定列表
            //this.BindList();
        }

        protected void IsTon_OnCheckedChanged(object sender, EventArgs e)
        {
            panelTon.Visible = IsTon.Checked;

            Tons.Text = string.Empty;
            Price.Text = string.Empty;
            Amt.Text = string.Empty;
        }

        #endregion

        #region 绑定数据

        /// <summary>
        /// 绑定列表
        /// </summary>
        private void BindList()
        {
            using (PurProvider purProvider = new PurProvider())
            {
                string dateFrom = LocalGlobal.ConvertDateFormat(DateFrom.Text.Trim()).ToString("yyyyMMdd");
                string dateTo = LocalGlobal.ConvertDateFormat(DateTo.Text.Trim()).ToString("yyyyMMdd");
                DataTable table = purProvider.GetPrStatement(OrNo.Text.Trim(), dateFrom, dateTo, VenNo.Text.Trim(), VenName.Text.Trim());
                List.DataSource = table;

                if (IsTon.Checked)
                {
                    List.Columns[11].Visible = false;
                    List.Columns[12].Visible = false;

                    var purTon = purProvider.GetPrTonByOrNo(OrNo.Text.Trim());
                    if (purTon != null)
                    {
                        Tons.Text = purTon["tons"].ToString();
                        Price.Text = purTon["price"].ToString();
                        Amt.Text = purTon["amt"].ToString();
                    }
                }
                else
                {
                    List.Columns[11].Visible = true;
                    List.Columns[12].Visible = true;
                }

                List.DataBind();
            }
        }

        /// <summary>
        /// 行绑定
        /// </summary>
        protected void List_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            switch (e.Row.RowType)
            {
                //数据行
                case DataControlRowType.DataRow:
                    #region 数据绑定

                    #endregion
                    break;

                case DataControlRowType.EmptyDataRow:
                case DataControlRowType.Header:
                case DataControlRowType.Separator:
                case DataControlRowType.Pager:
                case DataControlRowType.Footer:
                default:
                    break;
            }
        }

        /// <summary>
        /// 翻页
        /// </summary>
        protected void List_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            List.PageIndex = e.NewPageIndex;
            this.BindList();
        }

        #endregion

        #region 操作

        /// <summary>
        /// 确定
        /// </summary>
        protected void OKButton_Click(object sender, EventArgs e)
        {
            if (IsTon.Checked)
            {
                if (string.IsNullOrWhiteSpace(OrNo.Text))
                {
                    this.ShowWarningMessage("请输入工令号");
                    return;
                }
            }

            this.BindList();
        }

        /// <summary>
        /// 取消
        /// </summary>
        protected void CancelButton_Click(object sender, EventArgs e)
        {
            this.Initialize();
            List.DataSource = null;
            List.DataBind();
        }

        protected void ExportButton_Click(object sender, EventArgs e)
        {
            this.List.AllowPaging = false;
            this.BindList();
            LocalGlobal.ToExcel(this.List, "采购对账单");
            this.List.AllowPaging = true;
            this.BindList();
        }

        #endregion


    }
}